home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 3 / Cream of the Crop 3.iso / comm / wnos5src.zip / ENET.C < prev    next >
C/C++ Source or Header  |  1993-11-21  |  3KB  |  139 lines

  1. /* Stuff generic to all Ethernet controllers */
  2. #include <stdio.h>
  3. #include <dos.h>
  4. #include "global.h"
  5. #include "config.h"
  6. #ifdef ETHER
  7. #include "mbuf.h"
  8. #include "iface.h"
  9. #include "timer.h"
  10. #include "arp.h"
  11. #include "ip.h"
  12. #include "enet.h"
  13.  
  14. char Ether_bdcst[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  15.  
  16. /* Convert Ethernet header in host form to network mbuf */
  17. static struct mbuf *
  18. htonether(struct ether *ether,struct mbuf *data)
  19. {
  20.     struct mbuf *bp = pushdown(data,ETHERLEN);
  21.     char *cp = bp->data;
  22.  
  23.     memcpy(cp,ether->dest,EADDR_LEN);
  24.     cp += EADDR_LEN;
  25.     memcpy(cp,ether->source,EADDR_LEN);
  26.     cp += EADDR_LEN;
  27.     put16(cp,ether->type);
  28.  
  29.     return bp;
  30. }
  31.  
  32. /* Extract Ethernet header */
  33. int
  34. ntohether(struct ether *ether,struct mbuf **bpp)
  35. {
  36.     pullup(bpp,ether->dest,EADDR_LEN);
  37.     pullup(bpp,ether->source,EADDR_LEN);
  38.     ether->type = pull16(bpp);
  39.     return ETHERLEN;
  40. }
  41.  
  42. /* Format an Ethernet address into a printable ascii string */
  43. char *
  44. pether(char *out,char *addr)
  45. {
  46.     sprintf(out,"%02x:%02x:%02x:%02x:%02x:%02x",
  47.         uchar(addr[0]),uchar(addr[1]),
  48.         uchar(addr[2]),uchar(addr[3]),
  49.         uchar(addr[4]),uchar(addr[5]));
  50.     return out;
  51. }
  52.  
  53. /* Convert an Ethernet address from Hex/ASCII to binary */
  54. int
  55. gether(char *out,char *cp)
  56. {
  57.     int i;
  58.  
  59.     for(i = 6; i != 0; i--) {
  60.         *out++ = htoi(cp);
  61.         if((cp = strchr(cp,':')) == NULLCHAR)    /* Find delimiter */
  62.             break;
  63.         cp++;            /* and skip over it */
  64.     }
  65.     return i;
  66. }
  67.  
  68. /* Send an IP datagram on Ethernet */
  69. int
  70. enet_send(
  71. struct mbuf *bp,        /* Buffer to send */
  72. struct iface *iface,    /* Pointer to interface control block */
  73. int32 gateway,            /* IP address of next hop */
  74. int prec,
  75. int del,
  76. int tput,
  77. int rel)
  78. {
  79.     char *egate;
  80.  
  81.     if((egate = res_arp(iface,ARP_ETHER,gateway,bp)) != NULLCHAR)
  82.         return (*iface->output)(iface,egate,iface->hwaddr,IP_TYPE,bp);
  83.  
  84.     return 0;
  85. }
  86.  
  87. /* Send a packet with Ethernet header */
  88. int
  89. enet_output(
  90. struct iface *iface,    /* Pointer to interface control block */
  91. char *dest,                /* Destination Ethernet address */
  92. char *source,            /* Source Ethernet address */
  93. int16 type,                /* Type field */
  94. struct mbuf *data)        /* Data field */
  95. {
  96.     struct ether ep;
  97.     struct mbuf *bp;
  98.  
  99.     memcpy(ep.dest,dest,EADDR_LEN);
  100.     memcpy(ep.source,source,EADDR_LEN);
  101.  
  102.     ep.type = type;
  103.     bp = htonether(&ep,data);
  104.  
  105.     iface->lastsent = secclock();
  106.     iface->rawsndcnt++;
  107.     return (*iface->raw)(iface,bp);
  108. }
  109.  
  110. /* Process incoming Ethernet packets. Shared by all ethernet drivers. */
  111. void
  112. eproc(struct iface *iface,struct mbuf *bp)
  113. {
  114.     struct ether hdr;
  115.  
  116.     /* Remove Ethernet header and kick packet upstairs */
  117.     ntohether(&hdr,&bp);
  118.  
  119.     if(memcmp(hdr.dest,iface->hwaddr,EADDR_LEN) &&
  120.       memcmp(hdr.dest,Ether_bdcst,EADDR_LEN)){
  121.         free_p(bp);
  122.         return;
  123.     }
  124.  
  125.     switch(hdr.type){
  126.     case ARP_TYPE:
  127.     case REVARP_TYPE:
  128.         arp_input(iface,bp);
  129.         break;
  130.     case IP_TYPE:
  131.         ip_route(iface,NULLIF,bp,hdr.dest[0] & 1);
  132.         break;
  133.     default:
  134.         free_p(bp);
  135.         break;
  136.     }
  137. }
  138.  
  139. #endif /* ETHER */